package com.baobao.dao;

import com.baobao.domain.Course;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage Course entities.
 * 
 */
@Repository("CourseDAO")
@Transactional
public class CourseDAOImpl extends AbstractJpaDao<Course> implements CourseDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Course.class }));

	/**
	 * EntityManager injected by Spring for persistence unit BaoBao
	 *
	 */
	@PersistenceContext(unitName = "BaoBao")
	private EntityManager entityManager;

	/**
	 * Instantiates a new CourseDAOImpl
	 *
	 */
	public CourseDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findCourseByEndTimeContaining
	 *
	 */
	@Transactional
	public Set<Course> findCourseByEndTimeContaining(String endTime) throws DataAccessException {

		return findCourseByEndTimeContaining(endTime, -1, -1);
	}

	/**
	 * JPQL Query - findCourseByEndTimeContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Course> findCourseByEndTimeContaining(String endTime, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCourseByEndTimeContaining", startResult, maxRows, endTime);
		return new LinkedHashSet<Course>(query.getResultList());
	}

	/**
	 * JPQL Query - findAllCourses
	 *
	 */
	@Transactional
	public Set<Course> findAllCourses() throws DataAccessException {

		return findAllCourses(-1, -1);
	}

	/**
	 * JPQL Query - findAllCourses
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Course> findAllCourses(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllCourses", startResult, maxRows);
		return new LinkedHashSet<Course>(query.getResultList());
	}

	/**
	 * JPQL Query - findCourseByBanjiIdContaining
	 *
	 */
	@Transactional
	public Set<Course> findCourseByBanjiIdContaining(String banjiId) throws DataAccessException {

		return findCourseByBanjiIdContaining(banjiId, -1, -1);
	}

	/**
	 * JPQL Query - findCourseByBanjiIdContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Course> findCourseByBanjiIdContaining(String banjiId, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCourseByBanjiIdContaining", startResult, maxRows, banjiId);
		return new LinkedHashSet<Course>(query.getResultList());
	}

	/**
	 * JPQL Query - findCourseByWeekDay
	 *
	 */
	@Transactional
	public Set<Course> findCourseByWeekDay(Integer weekDay) throws DataAccessException {

		return findCourseByWeekDay(weekDay, -1, -1);
	}

	/**
	 * JPQL Query - findCourseByWeekDay
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Course> findCourseByWeekDay(Integer weekDay, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCourseByWeekDay", startResult, maxRows, weekDay);
		return new LinkedHashSet<Course>(query.getResultList());
	}

	/**
	 * JPQL Query - findCourseByNameContaining
	 *
	 */
	@Transactional
	public Set<Course> findCourseByNameContaining(String name) throws DataAccessException {

		return findCourseByNameContaining(name, -1, -1);
	}

	/**
	 * JPQL Query - findCourseByNameContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Course> findCourseByNameContaining(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCourseByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Course>(query.getResultList());
	}

	/**
	 * JPQL Query - findCourseByPrimaryKey
	 *
	 */
	@Transactional
	public Course findCourseByPrimaryKey(String banjiId, Integer weekDay, Integer seqNo) throws DataAccessException {

		return findCourseByPrimaryKey(banjiId, weekDay, seqNo, -1, -1);
	}

	/**
	 * JPQL Query - findCourseByPrimaryKey
	 *
	 */

	@Transactional
	public Course findCourseByPrimaryKey(String banjiId, Integer weekDay, Integer seqNo, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findCourseByPrimaryKey", startResult, maxRows, banjiId, weekDay, seqNo);
			return (com.baobao.domain.Course) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findCourseByEndTime
	 *
	 */
	@Transactional
	public Set<Course> findCourseByEndTime(String endTime) throws DataAccessException {

		return findCourseByEndTime(endTime, -1, -1);
	}

	/**
	 * JPQL Query - findCourseByEndTime
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Course> findCourseByEndTime(String endTime, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCourseByEndTime", startResult, maxRows, endTime);
		return new LinkedHashSet<Course>(query.getResultList());
	}

	/**
	 * JPQL Query - findCourseByStartTime
	 *
	 */
	@Transactional
	public Set<Course> findCourseByStartTime(String startTime) throws DataAccessException {

		return findCourseByStartTime(startTime, -1, -1);
	}

	/**
	 * JPQL Query - findCourseByStartTime
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Course> findCourseByStartTime(String startTime, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCourseByStartTime", startResult, maxRows, startTime);
		return new LinkedHashSet<Course>(query.getResultList());
	}

	/**
	 * JPQL Query - findCourseByBanjiId
	 *
	 */
	@Transactional
	public Set<Course> findCourseByBanjiId(String banjiId) throws DataAccessException {

		return findCourseByBanjiId(banjiId, -1, -1);
	}

	/**
	 * JPQL Query - findCourseByBanjiId
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Course> findCourseByBanjiId(String banjiId, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCourseByBanjiId", startResult, maxRows, banjiId);
		return new LinkedHashSet<Course>(query.getResultList());
	}

	/**
	 * JPQL Query - findCourseBySeqNo
	 *
	 */
	@Transactional
	public Set<Course> findCourseBySeqNo(Integer seqNo) throws DataAccessException {

		return findCourseBySeqNo(seqNo, -1, -1);
	}

	/**
	 * JPQL Query - findCourseBySeqNo
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Course> findCourseBySeqNo(Integer seqNo, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCourseBySeqNo", startResult, maxRows, seqNo);
		return new LinkedHashSet<Course>(query.getResultList());
	}

	/**
	 * JPQL Query - findCourseByStartTimeContaining
	 *
	 */
	@Transactional
	public Set<Course> findCourseByStartTimeContaining(String startTime) throws DataAccessException {

		return findCourseByStartTimeContaining(startTime, -1, -1);
	}

	/**
	 * JPQL Query - findCourseByStartTimeContaining
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Course> findCourseByStartTimeContaining(String startTime, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCourseByStartTimeContaining", startResult, maxRows, startTime);
		return new LinkedHashSet<Course>(query.getResultList());
	}

	/**
	 * JPQL Query - findCourseByName
	 *
	 */
	@Transactional
	public Set<Course> findCourseByName(String name) throws DataAccessException {

		return findCourseByName(name, -1, -1);
	}

	/**
	 * JPQL Query - findCourseByName
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Course> findCourseByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findCourseByName", startResult, maxRows, name);
		return new LinkedHashSet<Course>(query.getResultList());
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(Course entity) {
		return true;
	}
}
